package com.itfly.controller;


import com.itfly.entity.EventManagement;
import com.itfly.entity.Users;
import com.itfly.exception.BusinessException;
import com.itfly.result.Result;

import com.itfly.service.UserService;
import com.itfly.utils.JwtUtil;
import com.itfly.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/register")
    public Result register(@RequestBody Users user) {

        //查询用户
        Users u = userService.findByUserName(user.getUsername());
        if (u == null) {
            //没有占用
            //注册
            userService.register(user);
            return new Result<>(1,"注册成功",null);
        } else {
            //占用
            return new Result<>(0,"注册失败",null);
        }
    }

    @PostMapping("/login")
    public Result<String> login(@RequestParam("username") String username,@RequestParam("password") String password) {
        //根据用户名查询用户
        Users loginUser = userService.findByUserName(username);
        //判断该用户是否存在
        if (loginUser == null) {
           throw new BusinessException("该用户不存在");
        }

        System.out.println(username);
        System.out.println(password);
        //判断密码是否正确
        if(loginUser.getPassword().equals(password)){
            //登录成功
            Map<String, Object> claims = new HashMap<>();
            claims.put("id", loginUser.getId());
            claims.put("username", loginUser.getUsername());
            claims.put("name", loginUser.getName());
            String token = JwtUtil.genToken(claims);
            return new Result<>(1,"登录成功",token);
        }
        return new Result<>(0,"登录失败",null);
    }

    @GetMapping("/userInfo")
    public Result<Users> userInfo(@RequestHeader(name = "Authorization") String token) {
        //根据用户名查询用户
        Map<String, Object> map = JwtUtil.parseToken(token);
        String username = (String) map.get("username");
        Users user = userService.findByUserName(username);
        return new Result<>(1,"查询成功",user);
    }
    @GetMapping("/listInfo")
    public Result<List<Users>> usersInfoEvent(@RequestHeader(name = "Authorization") String token) {
        //根据用户名查询用户
//        System.out.println(token);
        Map<String, Object> map = JwtUtil.parseToken(token);
        Integer userId = (Integer) map.get("id");
        List<Users> list = userService.findAllUserInfo(userId);
        return new Result<>(1,"查询成功",list);
    }
    //模糊查询
    @PostMapping("/likeSearch")
    public Result<List<Users>> searchCategory(@RequestHeader(name = "Authorization") String token,@RequestBody Users users) {
        Map<String, Object> map = JwtUtil.parseToken(token);
        Integer userId = (Integer) map.get("id");
        users.setId(userId);
        List<Users> eventManagements = userService.selectByLike(users);
        return new Result<>(1,"查询成功",eventManagements);
    }

}
